#ifndef LEETCODE_CN_CPP_PROBLEMS_21_H
#define LEETCODE_CN_CPP_PROBLEMS_21_H

#include "utils/list.h"

namespace problem_21
{

class Solution
{
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        ListNode tmpHead(0);
        auto pre = &tmpHead;
        while (l1 != nullptr || l2 != nullptr)
        {
            if (l1 != nullptr && l2 != nullptr)
            {
                if (l1->val < l2->val)
                {
                    pre->next = l1;
                    l1 = l1->next;
                }
                else
                {
                    pre->next = l2;
                    l2 = l2->next;
                }
            }
            else if (l1 != nullptr)
            {
                pre->next = l1;
                l1 = l1->next;
            }
            else
            {
                pre->next = l2;
                l2 = l2->next;
            }
            pre = pre->next;
        }
        return tmpHead.next;
    }
};

} // namespace problem_21

#endif //LEETCODE_CN_CPP_PROBLEMS_21_H
